package com.bicon.botu.finance.payment.service;

import com.bicon.botu.finance.payment.alipay.AliPayService;
import com.bicon.botu.finance.payment.common.PaymentConstants;
import com.bicon.botu.finance.payment.entity.*;
import com.bicon.botu.finance.payment.weixin.api.WeChatPayService;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.finance.payment.repository.TransferRepository;
import com.bicon.botu.finance.payment.unionpay.UnionPayService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 提现
 *
 * @Author: 宋旭
 * @Date: Created in 15:04 2017/7/5/005
 */
@Service
@Log4j
public class TransferService {

    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private WeChatPayService weChatPayService;

    @Autowired
    private UnionPayService unionPayService;

    @Autowired
    private PayInfoService payInfoService;

    @Autowired
    private TransferRepository transferRepository;


    public TransferEntity findByOrderNo(String orderNo){
        return transferRepository.findByOrderNo(orderNo);
    }


    /**
     * 保存提现信息
     *
     * @param orderNo
     * @param payType
     * @param code
     * @param payDate
     */
    public void addTransfer(String orderNo, String payType, String code, Date payDate,String status) {
        //TransferEntity transferEntity = transferRepository.findByOrderNo(orderNo);
        TransferEntity transferEntity = new TransferEntity();
        transferEntity.setOrderNo(orderNo);
        transferEntity.setPayType(payType);
        transferEntity.setCode(code);
        transferEntity.setPayDate(payDate);
        transferEntity.setStatus(status);
        transferEntity.preInsert();
        transferRepository.save(transferEntity);
    }

    public void updateTransfer(String orderNo,String status){
        TransferEntity transferEntity = transferRepository.findByOrderNo(orderNo);
        transferEntity.setStatus(status);
        transferEntity.preUpdate();
        transferRepository.save(transferEntity);
    }

    /**
     * 提现接口
     * @param infoEntity
     *          第三方支付信息
     * @param type
     *          支付类型
     * @param orderNum
     *          订单号
     * @param payeeAccount
     *          提现账号
     * @param amount
     *          提现金额
     * @param payeeName
     *          提现人名称
     * @param remark
     *          备注
     * @return
     */
    public BaseResult transfer(AppPaymentInfoEntity infoEntity, String type, String orderNum, String payeeAccount,
                               String amount, String payeeName, String identityCard, String remark) {
        //金额参数校验
        float total = 0.0f;
        try {
            total = Float.parseFloat(amount);
        } catch (Exception e) {
            log.info("error", e);
            return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400, null);
        }
        boolean message = false;
        Date date = new Date();
        if (type.equals(AliPayDto.TYPE_ALIPAY)) {
            //支付宝转账
            //获取app在支付宝平台的相关信息
            AliPayDto aliPayDto = infoEntity.getAliPayDto();
            if (null == aliPayDto) {
                return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400, null);
            }
            message = aliPayService.transfer(infoEntity.getName(),aliPayDto.getAlipayAppId(),aliPayDto.getAlipayAppPublicKey(),aliPayDto.getAlipayAppPrivateKey(),
                    orderNum,payeeAccount,amount,payeeName,remark);
        } else if (type.equals(WechatPayDto.TYPE_WECHAT)) {
            //微信转账
            WechatPayDto wechatPayDto = infoEntity.getWechatPayDto();
            if (null == wechatPayDto) {
                return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400, null);
            }
            message = weChatPayService.transferToWeChat(wechatPayDto.getWechatAppId(),wechatPayDto.getWechatAppKey(),wechatPayDto.getWechatAppCertPath(),
                    wechatPayDto.getWechatAppPartner(),orderNum,payeeAccount,amount,payeeName,remark);
        } else if (type.equals(UnionpayDto.TYPE_UNION)) {
            UnionpayDto unionpayDto = infoEntity.getUnionpayDto();
            if (null == unionpayDto) {
                return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400, null);
            }
            message = unionPayService.daifuToUnion(unionpayDto.getUnionMerId(),orderNum,total,infoEntity.getCode(),
                    ""+PaymentConstants.UNIONPAY_TRA_NOTIFY,date,remark,identityCard,payeeName,payeeAccount);
        } else {
            return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400, null);
        }
        if (message) {
            addTransfer(orderNum,type,infoEntity.getCode(),date,PaymentConstants.TRANSFER_STATUS_SUCCESS);
            return new BaseResult(PaymentConstants.RESPONSE_CODE_200, PaymentConstants.RESPONSE_MESSAGE_200);
        }else {
            addTransfer(orderNum,type,infoEntity.getCode(),date,PaymentConstants.TRANSFER_STATUS_FAIL);
            return new BaseResult(PaymentConstants.RESPONSE_CODE_600, PaymentConstants.RESPONSE_MESSAGE_600, null);
        }
    }
}
